package cn.fl.collection.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.dto.SysOrgDTO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.utils.*;
import cn.fl.capital.condition.CapOverdueInfoQueryCondition;
import cn.fl.capital.constant.CapitalConstant;
import cn.fl.capital.service.ICapOverdueInfoService;
import cn.fl.capital.vo.CapOverdueInfoVO;
import cn.fl.collection.api.IRskCltAuthApiService;
import cn.fl.collection.api.IRskCltAuthRcvAplApiService;
import cn.fl.collection.condition.RskCltAuthQC;
import cn.fl.collection.constant.CollectionConstant;
import cn.fl.collection.dto.RskCltAuthDTO;
import cn.fl.collection.dto.RskCltAuthRcvAplDTO;
import cn.fl.collection.service.IRskCltAuthService;
import cn.fl.collection.transform.RskCltAuthRcvAplVOTransform;
import cn.fl.collection.transform.RskCltAuthVOTransform;
import cn.fl.collection.vo.RskCltAuthRcvAplVO;
import cn.fl.collection.vo.RskCltAuthVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description: 委外催收表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class RskCltAuthServiceImpl implements IRskCltAuthService {

	@Autowired
	private IRskCltAuthApiService rskCltAuthApiService;

	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Autowired
	private ICapOverdueInfoService capOverdueInfoService;

	@Autowired
	private ISysOrgApiService sysOrgApiService;

	@Autowired
    private IRskCltAuthRcvAplApiService rskCltAuthRcvAplApiService;

	@Override
	public RskCltAuthVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.newLogger("主键参数不能为空.", CurrentThreadContext.getCurrentUserName()).setKeys("selectById").info();
			return null;
		}
		RskCltAuthDTO dto = rskCltAuthApiService.selectById(id);
		return RskCltAuthVOTransform.toVO(dto);
	}

	@Override
	public Long add(RskCltAuthVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.newLogger("新增对象不能为空.", CurrentThreadContext.getCurrentUserName()).setKeys("add").info();
			return null;
		}
		return this.rskCltAuthApiService.addRskCltAuth(RskCltAuthVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.newLogger("删除主键参数不能为空.", CurrentThreadContext.getCurrentUserName()).setKeys("removeById").info();
			return false;
		}
		//return this.rskCltAuthApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(RskCltAuthVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.newLogger("修改对象参数不能为空.", CurrentThreadContext.getCurrentUserName()).setKeys("modify").info();
			return false;
		}
		return this.rskCltAuthApiService.modify(RskCltAuthVOTransform.toDTO(entity));
	}


	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		return this.rskCltAuthApiService.searchListPage(page);
	}

	@Override
	public Pager searchRskCltAuthListPage(Pager page) throws BaseException {
		if(null == page)page = new Pager();
		BaseLogUtils.newLogger("分页查询委外分案数据.", CurrentThreadContext.getCurrentUserName()).setKeys("searchRskCltAuthListPage").info();
		page = this.rskCltAuthApiService.selectRskCltAuthListPage(page);
		List<RskCltAuthDTO> rskCltAuthDTOS = (List<RskCltAuthDTO>)page.getResultList();
		List<RskCltAuthVO> rskCltAuthVOS = RskCltAuthVOTransform.toVOList(rskCltAuthDTOS);
		if(CollectionUtils.isNotEmpty(rskCltAuthVOS)){
			//添加省公司 事业部 剩余租金等信息
			//剩余租金
			Long residualRent = null;
			//租金合计
			Long totalRentAmt = null;
			//违约金
			Double breachAmt = null;
			//委案金额=应收金额 + 剩余租金 + 违约金
			Double authAmt = null;

			for(RskCltAuthVO rskCltAuthVO : rskCltAuthVOS){
				residualRent = this.rskCltAuthApiService.getTotalRentAmtByPayPd(rskCltAuthVO.getPaySchId(),rskCltAuthVO.getPayPd());
				BaseLogUtils.newLogger("查询剩余租金").setKeys("searchRskCltAuthListPage","residualRent="+residualRent,"paySchId="+rskCltAuthVO.getPaySchId(),"payPd="+rskCltAuthVO.getPayPd()).info();
				if(residualRent == null){
					//逾期期次未最后一次时候，剩余租金为0
					residualRent = 0L;
				}
				if(CapitalConstant.CODE_OVD_STATUS_CD_2.equals(rskCltAuthVO.getOverdueStatusCd())){
					rskCltAuthVO.setOverdueDay(0);
					rskCltAuthVO.setArAmt(0L);
				}
				//只有租金未还请并且未委案的数据才需要计算
//				if(null != rskCltAuthVO.getArAmt() && rskCltAuthVO.getArAmt() > 0
//					&& (null == rskCltAuthVO.getAuthStsCd() || 0 != rskCltAuthVO.getAuthStsCd())){
//					totalRentAmt = this.rskCltAuthApiService.getTotalRentAmtByPaySchId(rskCltAuthVO.getPaySchId());
//					BaseLogUtils.newLogger("查询租金合计").setKeys("searchRskCltAuthListPage","totalRentAmt="+totalRentAmt,"paySchId="+rskCltAuthVO.getPaySchId(),"payPd="+rskCltAuthVO.getPayPd()).info();
//					breachAmt = DecimalUtils.round(DecimalUtils.mul(totalRentAmt.doubleValue(),0.08D),0);
//					BaseLogUtils.newLogger("查询违约金").setKeys("searchRskCltAuthListPage","breachAmt="+breachAmt,"paySchId="+rskCltAuthVO.getPaySchId(),"payPd="+rskCltAuthVO.getPayPd()).info();
//					authAmt = DecimalUtils.round(DecimalUtils.add(DecimalUtils.add(residualRent.doubleValue(),rskCltAuthVO.getArAmt().doubleValue()),breachAmt),0);
//					BaseLogUtils.newLogger("查询委案金额").setKeys("searchRskCltAuthListPage","authAmt="+authAmt,"paySchId="+rskCltAuthVO.getPaySchId(),"payPd="+rskCltAuthVO.getPayPd()).info();
//					rskCltAuthVO.setAuthAmt(authAmt.longValue());
//					rskCltAuthVO.setBreachAmt(breachAmt.longValue());
//
//				}
				List<KeyValueVO> keyValueVOS = sysDictionaryApiService.getKvList(CollectionConstant.BREACH_AMT_PERCENT,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
				Double breachAmtPercent = DecimalUtils.div(Double.valueOf(keyValueVOS.get(0).getKey()),100D,2);
				totalRentAmt = this.rskCltAuthApiService.getTotalRentAmtByPaySchId(rskCltAuthVO.getPaySchId());
				BaseLogUtils.newLogger("查询租金合计").setKeys("searchRskCltAuthListPage","totalRentAmt="+totalRentAmt,"paySchId="+rskCltAuthVO.getPaySchId(),"payPd="+rskCltAuthVO.getPayPd()).info();
				breachAmt = DecimalUtils.round(DecimalUtils.mul(totalRentAmt.doubleValue(),breachAmtPercent),0);
				BaseLogUtils.newLogger("查询违约金").setKeys("searchRskCltAuthListPage","breachAmt="+breachAmt,"paySchId="+rskCltAuthVO.getPaySchId(),"payPd="+rskCltAuthVO.getPayPd()).info();
				authAmt = DecimalUtils.round(DecimalUtils.add(DecimalUtils.add(residualRent.doubleValue(),rskCltAuthVO.getArAmt().doubleValue()),breachAmt),0);
				BaseLogUtils.newLogger("查询委案金额").setKeys("searchRskCltAuthListPage","authAmt="+authAmt,"paySchId="+rskCltAuthVO.getPaySchId(),"payPd="+rskCltAuthVO.getPayPd()).info();
				rskCltAuthVO.setAuthAmt(authAmt.longValue());
				rskCltAuthVO.setBreachAmt(breachAmt.longValue());

				//省公司
				String cstMgrOrgCdNm = null;
				if(StringUtils.isNotEmpty(rskCltAuthVO.getCstMgrOrgCd())){
					cstMgrOrgCdNm = this.getCstPrOrgCdNm(rskCltAuthVO.getCstMgrOrgCd());
				}
				//事业部
				String buOrgNm = null;
				if(StringUtils.isNotEmpty(rskCltAuthVO.getBuOrgCd())){
					buOrgNm = sysOrgApiService.selectSysOrgNmByOrgCd(rskCltAuthVO.getBuOrgCd(),CurrentThreadContext.getCurrentSysCd());
				}
				rskCltAuthVO.setResidualRent(residualRent);
				rskCltAuthVO.setCstMgrOrgCdNm(cstMgrOrgCdNm);
				rskCltAuthVO.setBuOrgNm(buOrgNm);
				if(StringUtils.isNotEmpty(rskCltAuthVO.getSueStsCd())){
					rskCltAuthVO.setSueStsCdNm(getSueStsCdNm(rskCltAuthVO.getSueStsCd()));
				}
			}
			//集合排序
			rskCltAuthVOS.sort(Comparator.comparing(RskCltAuthVO::getStartDate,Comparator.nullsFirst(Date::compareTo)).reversed());


		}
		BaseLogUtils.newLogger("分页查询委外分案数据结束.", CurrentThreadContext.getCurrentUserName()).setKeys("searchRskCltAuthListPage").info();
		page.setResultList(rskCltAuthVOS);
		return page;
	}

	@Override
	public DataResultVO verifyRskCltAuth(RskCltAuthVO entity,DataResultVO result) {
		//检查是否存在委案中数据
		RskCltAuthQC qc = new RskCltAuthQC();
		qc.setPaySchId(entity.getPaySchId());
		qc.setSortName("AUTH_STS_CD");
		qc.setSortOrder("ASC");
		List<RskCltAuthDTO> rskCltAuthDTOS = this.rskCltAuthApiService.searchList(qc);
		BaseLogUtils.newLogger("查询委案数据").setKeys("verifyRskCltAuth","rskCltAuthDTOSSIze="+rskCltAuthDTOS.size(),"qc="+ JSON.toJSONString(qc)).info();
		if(CollectionUtils.isNotEmpty(rskCltAuthDTOS)){
			if(null != entity.getAuthStsCd() && entity.getAuthStsCd() == 0){
				//打开委案页面时存在委案中数据，添加时数据不存在了，视为数据不一致
				for(RskCltAuthDTO rskCltAuthDTO : rskCltAuthDTOS){
					if(0 == rskCltAuthDTO.getAuthStsCd()){
						result.setSuccess(BaseConstant.IS_YESNO_NO);
						result.setInfo("委案失败，委案状态变化");
						return result;
					}
				}
			}else{
				for(RskCltAuthDTO rskCltAuthDTO : rskCltAuthDTOS){
					if(0 ==rskCltAuthDTO.getAuthStsCd()){
						result.setSuccess(BaseConstant.IS_YESNO_NO);
						result.setInfo("支付表存在委案中数据");
						return result;
					}
				}
			}

		}else{
			if(null != entity.getAuthStsCd() && entity.getAuthStsCd() == 0){
				//打开委案页面时存在委案中数据，添加时数据不存在了，视为数据不一致
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("委案失败，委案状态变化");
				return result;
			}
		}
		//查询逾期信息 核对委案金额
		CapOverdueInfoQueryCondition capOverdueInfoQueryCondition = new CapOverdueInfoQueryCondition();
		capOverdueInfoQueryCondition.setPaySchId(entity.getPaySchId());
		List<CapOverdueInfoVO> capOverdueInfoVOS = capOverdueInfoService.searchList(capOverdueInfoQueryCondition);
		if(CollectionUtils.isEmpty(capOverdueInfoVOS)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("支付表不存在逾期数据");
			return result;
		}
		//计算委案金额 = 应收租金 + 剩余租金 + 违约金
		CapOverdueInfoVO capOverdueInfoVO = capOverdueInfoVOS.get(0);
		BaseLogUtils.newLogger("查询支付表逾期数据").setKeys("verifyRskCltAuth","capOverdueInfoVO="+JSON.toJSONString(capOverdueInfoVO),"capOverdueInfoQueryCondition="+ JSON.toJSONString(capOverdueInfoQueryCondition)).info();
		//剩余租金
		Long residualRent = this.rskCltAuthApiService.getTotalRentAmtByPayPd(capOverdueInfoVO.getPaySchId(),capOverdueInfoVO.getPayPd().intValue());
		BaseLogUtils.newLogger("查询剩余租金").setKeys("verifyRskCltAuth","residualRent="+residualRent,"paySchId="+capOverdueInfoVO.getPaySchId(),"payPd="+capOverdueInfoVO.getPayPd()).info();
		if(residualRent == null){
			//逾期期次未最后一次时候，剩余租金为0
			residualRent = 0L;
		}
		//租金合计
		Long totalRentAmt = this.rskCltAuthApiService.getTotalRentAmtByPaySchId(capOverdueInfoVO.getPaySchId());
		BaseLogUtils.newLogger("查询租金合计").setKeys("verifyRskCltAuth","totalRentAmt="+totalRentAmt,"paySchId="+capOverdueInfoVO.getPaySchId()).info();
		//违约金
		List<KeyValueVO> keyValueVOS = sysDictionaryApiService.getKvList(CollectionConstant.BREACH_AMT_PERCENT,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
		Double breachAmtPercent = DecimalUtils.div(Double.valueOf(keyValueVOS.get(0).getKey()),100D,2);
		Double breachAmt = DecimalUtils.round(DecimalUtils.mul(totalRentAmt.doubleValue(),breachAmtPercent),0);
		BaseLogUtils.newLogger("查询违约金").setKeys("verifyRskCltAuth","breachAmt="+breachAmt).info();

		//应收租金
		Long arAmt = capOverdueInfoVO.getOverdueAmount() + capOverdueInfoVO.getPenaltyAmount();
		//如果逾期租金全部支付完 设置逾期天数为0
		if(CapitalConstant.CODE_OVD_STATUS_CD_2.equals(capOverdueInfoVO.getOverdueStatusCd())){
			entity.setOverdueDay(0);
			entity.setArAmt(0L);
		}else{
			entity.setOverdueDay(capOverdueInfoVO.getOverdueDay().intValue());
		}
		//委案金额=应收金额 + 剩余租金 + 违约金
		Double authAmt = DecimalUtils.round(DecimalUtils.add(DecimalUtils.add(residualRent.doubleValue(),arAmt.doubleValue()),breachAmt),0);
		BaseLogUtils.newLogger("计算委案金额").setKeys("verifyRskCltAuth","authAmt="+authAmt).info();
		if(authAmt.longValue() != entity.getAuthAmt().longValue()){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("委案失败,委案金额变化!");
			BaseLogUtils.newLogger("委案金额计算不一致").setKeys("verifyRskCltAuth","authAmt="+authAmt).info();
			return result;
		}
		entity.setAuthStsCd(0);
		//查询委案次数
		entity.setAuthCnt(rskCltAuthDTOS.size() + 1);
		result.setSuccess(BaseConstant.IS_YESNO_YES);
		return result;
	}

	@Override
	public RskCltAuthVO selectRskCltAuthByPaySchId(RskCltAuthQC rskCltAuthQC) {
    	RskCltAuthVO rskCltAuthVO = new RskCltAuthVO();
    	Long paySchId = rskCltAuthQC.getPaySchId();
		rskCltAuthQC.setPaySchId(null);
    	//查询是否有委外中记录
		rskCltAuthQC.setAuthStsCd(0);
		List<RskCltAuthDTO> rskCltAuthDTOS = this.rskCltAuthApiService.searchList(rskCltAuthQC);
		if(CollectionUtils.isNotEmpty(rskCltAuthDTOS)){
			rskCltAuthVO.setAuthOrgCd(rskCltAuthDTOS.get(0).getAuthOrgCd());
			rskCltAuthVO.setAuthStsCd(0);
		}
		CapOverdueInfoQueryCondition capOverdueInfoQueryCondition = new CapOverdueInfoQueryCondition();
		capOverdueInfoQueryCondition.setPaySchId(paySchId);
		List<CapOverdueInfoVO> capOverdueInfoVOS = capOverdueInfoService.searchList(capOverdueInfoQueryCondition);
		if(CollectionUtils.isNotEmpty(capOverdueInfoVOS)){
			CapOverdueInfoVO capOverdueInfoVO = capOverdueInfoVOS.get(0);
			//剩余租金
			Long residualRent = null;
			//应收租金
			Long arAmt = null;
			BaseLogUtils.newLogger("查询剩余租金").setKeys("selectRskCltAuthByPaySchId","residualRent="+residualRent,"paySchId="+capOverdueInfoVO.getPaySchId(),"payPd="+capOverdueInfoVO.getPayPd()).info();
			if(CapitalConstant.CODE_OVD_STATUS_CD_2.equals(capOverdueInfoVO.getOverdueStatusCd())){
				arAmt = 0L;
				residualRent = this.rskCltAuthApiService.getTotalRentAmtByPayPd(capOverdueInfoVO.getPaySchId(),null);
			}else{
				arAmt = capOverdueInfoVO.getOverdueAmount() + capOverdueInfoVO.getPenaltyAmount();
				residualRent = this.rskCltAuthApiService.getTotalRentAmtByPayPd(capOverdueInfoVO.getPaySchId(),capOverdueInfoVO.getPayPd().intValue());
			}
			if(residualRent == null){
				//逾期期次未最后一次时候，剩余租金为0
				residualRent = 0L;
			}
			//租金合计
			Long totalRentAmt = this.rskCltAuthApiService.getTotalRentAmtByPaySchId(capOverdueInfoVO.getPaySchId());
			BaseLogUtils.newLogger("查询租金合计").setKeys("selectRskCltAuthByPaySchId","totalRentAmt="+totalRentAmt,"paySchId="+capOverdueInfoVO.getPaySchId()).info();

			//违约金
			List<KeyValueVO> keyValueVOS = sysDictionaryApiService.getKvList(CollectionConstant.BREACH_AMT_PERCENT,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
			Double breachAmtPercent = DecimalUtils.div(Double.valueOf(keyValueVOS.get(0).getKey()),100D,2);
			Double breachAmt = DecimalUtils.round(DecimalUtils.mul(totalRentAmt.doubleValue(),breachAmtPercent),0);
			BaseLogUtils.newLogger("查询违约金").setKeys("selectRskCltAuthByPaySchId","breachAmt="+breachAmt).info();


			BaseLogUtils.newLogger("查询应收租金").setKeys("selectRskCltAuthByPaySchId","arAmt="+arAmt).info();

			//委案金额=应收金额 + 剩余租金 + 违约金
			Double authAmt = DecimalUtils.round(DecimalUtils.add(DecimalUtils.add(residualRent.doubleValue(),arAmt.doubleValue()),breachAmt),0);
			rskCltAuthVO.setPaySchId(capOverdueInfoVO.getPaySchId());
			rskCltAuthVO.setPaySchNo(capOverdueInfoVO.getPaySchNo());
			rskCltAuthVO.setCstId(capOverdueInfoVO.getCstId());
			rskCltAuthVO.setCstNm(capOverdueInfoVO.getCstNm());
			rskCltAuthVO.setResidualRent(residualRent);
			rskCltAuthVO.setBreachAmt(breachAmt.longValue());
			rskCltAuthVO.setAuthAmt(authAmt.longValue());
			rskCltAuthVO.setStartDate(new Date());
			rskCltAuthVO.setArAmt(arAmt);
		}
		return rskCltAuthVO;
	}

	@Override
	public List<RskCltAuthVO> searchList(RskCltAuthQC rskCltAuthQC) {
		return 	RskCltAuthVOTransform.toVOList(this.rskCltAuthApiService.searchList(rskCltAuthQC));
	}

	@Override
	public Pager searchRskCltAuthCollectionListPage(Pager page) throws BaseException {
		if (null == page) page = new Pager();
		page = this.rskCltAuthApiService.selectRskCltAuthCollectionListPage(page);
		List<RskCltAuthDTO> rskCltAuthDTOS = (List<RskCltAuthDTO>) page.getResultList();
		List<RskCltAuthVO> rskCltAuthVOS = RskCltAuthVOTransform.toVOList(rskCltAuthDTOS);
		if (CollectionUtils.isNotEmpty(rskCltAuthVOS)) {
			//添加省公司 事业部 剩余租金等信息
			Map<String, String> orgMap = Maps.newHashMap();
			for (RskCltAuthVO rskCltAuthVO : rskCltAuthVOS) {
				//省公司
				if (StringUtils.isNotEmpty(rskCltAuthVO.getCstMgrOrgCd())) {
					if (!orgMap.containsKey(rskCltAuthVO.getCstMgrOrgCd())) {
						String cstMgrOrgCdNm = sysOrgApiService.selectSysOrgNmByOrgCd(rskCltAuthVO.getCstMgrOrgCd(), CurrentThreadContext.getCurrentSysCd());
						orgMap.put(rskCltAuthVO.getCstMgrOrgCd(), cstMgrOrgCdNm);
					}
					rskCltAuthVO.setCstMgrOrgCdNm(orgMap.get(rskCltAuthVO.getCstMgrOrgCd()));
				}
				//事业部
				if (StringUtils.isNotEmpty(rskCltAuthVO.getBuOrgCd())) {
					if (!orgMap.containsKey(rskCltAuthVO.getBuOrgCd())) {
						String buOrgNm = sysOrgApiService.selectSysOrgNmByOrgCd(rskCltAuthVO.getBuOrgCd(), CurrentThreadContext.getCurrentSysCd());
						orgMap.put(rskCltAuthVO.getBuOrgCd(), buOrgNm);
					}
					rskCltAuthVO.setBuOrgNm(orgMap.get(rskCltAuthVO.getBuOrgCd()));
				}
				//剩余天数
				if (null != rskCltAuthVO.getEndDate()) {
					Long residueAuthDay = DateUtils.getDaysBetweenTwoDates(new Date(), rskCltAuthVO.getEndDate());
					rskCltAuthVO.setResidueAuthDay(residueAuthDay.intValue());
				}
			}
		}
		page.setResultList(rskCltAuthVOS);
		return page;
	}

	private String getSueStsCdNm(String sueStsCd){
    	String sueStsCdNm = null;
		List<KeyValueVO> kvList = sysDictionaryApiService.getKvList("SUE0007","11000","0000");
		for(KeyValueVO keyValueVO : kvList){
			if(sueStsCd.equals(keyValueVO.getKey())){
				sueStsCdNm = keyValueVO.getValue();
				break;
			}
		}
		return sueStsCdNm;
	}

    @Override
    public RskCltAuthRcvAplVO getCltAuthApl4Flow(Long applyId) throws BaseException {
        try {
            RskCltAuthRcvAplDTO rskCltAuthRcvAplDTO = this.rskCltAuthRcvAplApiService.selectApprovalProcessData(applyId);
            if (null == rskCltAuthRcvAplDTO) {
                throw new BaseException("无法查询委外分案申请信息");
            }
            RskCltAuthDTO rskCltAuthDTO = rskCltAuthRcvAplDTO.getRskCltAuthDTO();
            if (null == rskCltAuthDTO) {
                throw new BaseException("无法查询委外分案申请信息");
            }
            RskCltAuthRcvAplVO rskCltAuthRcvAplVO = RskCltAuthRcvAplVOTransform.toVO(rskCltAuthRcvAplDTO);
            rskCltAuthRcvAplVO.setCstNm(rskCltAuthDTO.getCstNm());
            rskCltAuthRcvAplVO.setPaySchNo(rskCltAuthDTO.getPaySchNo());
            rskCltAuthRcvAplVO.setAuthAmt(rskCltAuthDTO.getAuthAmt());
            rskCltAuthRcvAplVO.setOvdDay(rskCltAuthDTO.getOverdueDay());
            rskCltAuthRcvAplVO.setAuthDt(rskCltAuthDTO.getStartDate());
            return rskCltAuthRcvAplVO;
        } catch (BaseException e) {
            BaseLogUtils.newLogger("查询委外分案数据失败").setKeys("applyId:" + applyId).setThrowable(e).setWarning(false).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        } catch (Exception e) {
            BaseLogUtils.newLogger("查询委外分案数据失败").setKeys("applyId:" + applyId).setThrowable(e).setWarning(false).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        }
    }

    @Override
    public boolean modifyRskCltAuthRcvApl(RskCltAuthRcvAplVO rskCltAuthRcvAplVO) throws BaseException {
        return this.rskCltAuthRcvAplApiService.modifyById(RskCltAuthRcvAplVOTransform.toDTO(rskCltAuthRcvAplVO));
    }

	/**
	 * 获取省公司名称
	 */
	private String getCstPrOrgCdNm(String cstMgrOrgCd){
		String cstPrOrgCdNm = null;
		List<SysOrgDTO> list = this.sysOrgApiService.searchSysOrgListBy(CurrentThreadContext.getCurrentSysCd());
		Map<String, SysOrgDTO> buOrgCdMap = new HashMap<>();
		if (CollectionUtils.isNotEmpty(list)) {
			for (SysOrgDTO sysOrgDTO : list) {
				buOrgCdMap.put(sysOrgDTO.getOrgCd(), sysOrgDTO);
			}
		}
		SysOrgDTO sysOrgDTO = null;
		String orgCd = null;
		if (buOrgCdMap.containsKey(cstMgrOrgCd)) {
			sysOrgDTO = buOrgCdMap.get(cstMgrOrgCd);
			orgCd = sysOrgDTO.getOrgCd();
			while(orgCd.length()>8){
				if("3".equals(sysOrgDTO.getOrgTypCd())){//分公司
					cstPrOrgCdNm = sysOrgDTO.getOrgNm();
					break;
				}
				orgCd = orgCd.substring(0,orgCd.length()-4);
				if(buOrgCdMap.containsKey(orgCd)){
					sysOrgDTO = buOrgCdMap.get(orgCd);
				}else{
					break;
				}
			}
		}
		return cstPrOrgCdNm;
	}
}
